package com.atguigu.lease.common.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        //创建RedisTemplate对象
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(connectionFactory);//设置连接工厂对象

        //设置序列化器
        StringRedisSerializer stringRedisSerializer = StringRedisSerializer.UTF_8;
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();//创建JSON序列化器
        //设置RedisTemplate对象
        redisTemplate.setKeySerializer(stringRedisSerializer);//设置key的序列化器
        redisTemplate.setHashKeySerializer(stringRedisSerializer);//设置HashKey的序列化器
        redisTemplate.setValueSerializer(jsonRedisSerializer);//设置value的序列化器
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);//设置HashValue的序列化器

        return redisTemplate;
    }
}
